'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const q = require('../nodes/identity.cjs'),
  L = require('../nodes/Scalar.cjs'),
  v = require('../nodes/YAMLMap.cjs'),
  A = require('../nodes/YAMLSeq.cjs'),
  S = require('./resolve-block-map.cjs'),
  d = require('./resolve-block-seq.cjs'),
  M = require('./resolve-flow-collection.cjs');
function f(r, c, o, a, t, l) {
  const i =
      o.type === 'block-map'
        ? S.resolveBlockMap(r, c, o, a, l)
        : o.type === 'block-seq'
          ? d.resolveBlockSeq(r, c, o, a, l)
          : M.resolveFlowCollection(r, c, o, a, l),
    s = i.constructor;
  return t === '!' || t === s.tagName
    ? ((i.tag = s.tagName), i)
    : (t && (i.tag = t), i);
}
function b(r, c, o, a, t) {
  var m;
  const l = a
      ? c.directives.tagName(a.source, e => t(a, 'TAG_RESOLVE_FAILED', e))
      : null,
    i =
      o.type === 'block-map'
        ? 'map'
        : o.type === 'block-seq'
          ? 'seq'
          : o.start.source === '{'
            ? 'map'
            : 'seq';
  if (
    !a ||
    !l ||
    l === '!' ||
    (l === v.YAMLMap.tagName && i === 'map') ||
    (l === A.YAMLSeq.tagName && i === 'seq') ||
    !i
  )
    return f(r, c, o, t, l);
  let s = c.schema.tags.find(e => e.tag === l && e.collection === i);
  if (!s) {
    const e = c.schema.knownTags[l];
    if (e && e.collection === i)
      c.schema.tags.push(Object.assign({}, e, { default: !1 })), (s = e);
    else
      return (
        e != null && e.collection
          ? t(
              a,
              'BAD_COLLECTION_TYPE',
              `${e.tag} used for ${i} collection, but expects ${e.collection}`,
              !0,
            )
          : t(a, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${l}`, !0),
        f(r, c, o, t, l)
      );
  }
  const n = f(r, c, o, t, l, s),
    p =
      ((m = s.resolve) == null
        ? void 0
        : m.call(s, n, e => t(a, 'TAG_RESOLVE_FAILED', e), c.options)) ?? n,
    u = q.isNode(p) ? p : new L.Scalar(p);
  return (
    (u.range = n.range),
    (u.tag = l),
    s != null && s.format && (u.format = s.format),
    u
  );
}
exports.composeCollection = b;
